class BlackDeathPickUp extends Actor
	abstract;
	
var() class<Inventory>				InventoryType;//Which inventory you wish to spawn
var	transient PrimitiveComponent	PickupMesh;

simulated event PreBeginPlay()
{
	InitializePickup();
	Super.PreBeginPlay();
}

simulated function InitializePickup()
{
	if ( InventoryType == None )
	{
		`Warn("No inventory type for" @ self);
		return;
	}

	SetPickupMesh();
}

simulated event SetInitialState()
{
	bScriptInitialized = true;
	if (InventoryType == None)
	{
		`warn("Disabling as no inventory type for " $ self);
		GotoState('Disabled');
	}
	else
	{
		Super.SetInitialState();
	}
}

simulated function SetPickupMesh()
{
	if ( PickupMesh != None )
	{
		if (PickupMesh.HiddenGame)
		{
			PickupMesh.SetHidden(false);
		}
	}
}

function Reset()
{
	GotoState('Pickup');
	Super.Reset();
}

/*function bool CheckForErrors()
{
	local Actor HitActor;
	local vector HitLocation, HitNormal;

	HitActor = Trace(HitLocation, HitNormal,Location - Vect(0,0,10), Location,false);
	if ( HitActor == None )
	{
		`log(self$" FLOATING");
		return true;
	}
	return Super.CheckForErrors();
}*/

function SpawnCopyFor( Pawn Recipient )
{
	local Inventory Inv;

	Inv = spawn(InventoryType);
	if ( Inv != None )
	{
		Inv.GiveTo(Recipient);
		Inv.AnnouncePickup(Recipient);
	}
}

function GiveTo( Pawn P )
{
	SpawnCopyFor(P);
	PickedUpBy(P);
}

function PickedUpBy(Pawn P)
{
	TriggerEventClass(class'SeqEvent_PickupStatusChange', P, 1);
	DetachComponent(PickupMesh);
	GotoState('Disabled');
}

auto state Pickup
{
	/* DetourWeight()
	value of this path to take a quick detour (usually 0, used when on route to distant objective, but want to grab inventory for example)
	*/
	event float DetourWeight(Pawn Other,float PathWeight)
	{
		return InventoryType.static.DetourWeight(Other,PathWeight);
	}

	/*
	 Validate touch (if valid return true to let other pick me up and trigger event).
	*/
	function bool ValidTouch( Pawn Other )
	{
		// make sure its a live player
		if (Other == None || !Other.bCanPickupInventory)
		{
			return false;
		}
		else if (Other.Controller == None)
		{
			// re-check later in case this Pawn is in the middle of spawning, exiting a vehicle, etc
			// and will have a Controller shortly
			SetTimer( 0.2, false, nameof(RecheckValidTouch) );
			return false;
		}
		// make sure not touching through wall
		else if ( !FastTrace(Other.Location, Location) )
		{
			SetTimer( 0.5, false, nameof(RecheckValidTouch) );
			return false;
		}
		
		// make sure game will let player pick me up
		if (WorldInfo.Game.PickupQuery(Other, InventoryType, self))
		{
			return true;
		}
		return false;
	}

	/**
	Pickup was touched through a wall.  Check to see if touching pawn is no longer obstructed
	*/
	function RecheckValidTouch()
	{
		CheckTouching();
	}

	// When touched by an actor.
	event Touch( Actor Other, PrimitiveComponent OtherComp, vector HitLocation, vector HitNormal )
	{
		local Pawn P;

		// If touched by a player pawn, let him pick this up.
		P = Pawn(Other);

		if( P != None && ValidTouch(P) && BlackDeathHeroPawn(P) != none )
		{
			GiveTo(P);
 		}
	}

	// Make sure no pawn already touching (while touch was disabled in sleep).
	function CheckTouching()
	{
		local Pawn P;

		ForEach TouchingActors(class'Pawn', P)
			Touch(P, None, Location, Normal(Location-P.Location) );
	}

	event BeginState(name PreviousStateName)
	{
		TriggerEventClass(class'SeqEvent_PickupStatusChange', None, 0);
	}

Begin:
	CheckTouching();
}

/** 
  * Make pickup mesh invisible.
  */
simulated function SetPickupHidden()
{
	bForceNetUpdate = TRUE;
	PickupMesh.SetHidden(true);
}

/** 
  * Make pickup mesh visible.
  */
simulated function SetPickupVisible()
{
	bForceNetUpdate = TRUE;
	PickupMesh.SetHidden(false);
}

State Disabled
{
	//function Reset() {}
	function StartSleeping() {}

	simulated event SetInitialState()
	{
		bScriptInitialized = true;
	}

	simulated event BeginState(Name PreviousStateName)
	{
		SetPickupHidden();
		SetCollision(false,false);
	}

	simulated event EndState(Name NextStateName)
	{
		SetPickupVisible();
	}
}

defaultproperties
{
	TickGroup=TG_DuringAsyncWork
	
	Begin Object Class=CylinderComponent Name=CollisionCylinder
		CollisionRadius=00020.000000
		CollisionHeight=00040.000000
		CollideActors=true
	End Object
	CollisionComponent=CollisionCylinder
	Components.Add(CollisionCylinder)

	Begin Object Class=DynamicLightEnvironmentComponent Name=MyLightEnvironment
		ModShadowFadeoutTime=0.25
		MinTimeBetweenFullUpdates=0.2
		AmbientGlow=(R=.01,G=.01,B=.01,A=1)
		AmbientShadowColor=(R=0.15,G=0.15,B=0.15)
		bSynthesizeSHLight=TRUE
	End Object
	Components.Add(MyLightEnvironment)
	
	Begin Object Class=SkeletalMeshComponent Name=EditorMesh
		bOnlyOwnerSee=false
		HiddenGame=True
		CollideActors=false
		AlwaysLoadOnClient=true
		AlwaysLoadOnServer=true
		MaxDrawDistance=4000
		bForceRefPose=1
		bUpdateSkelWhenNotRendered=false
		bIgnoreControllersWhenNotRendered=true
		bOverrideAttachmentOwnerVisibility=true
		bAcceptsDynamicDecals=FALSE
		bAllowAmbientOcclusion=false
		LightEnvironment=MyLightEnvironment;
	End Object
	Components.Add(EditorMesh)
	PickupMesh=EditorMesh;
	
	//Components.Remove(Sprite)

	bCollideWhenPlacing=False
	bHiddenEd=false
	bStatic=false
	bNoDelete=true
	RemoteRole=ROLE_SimulatedProxy
	bAlwaysRelevant=true
	bCollideActors=true
	bCollideWorld=false
	bBlockActors=false
	bIgnoreEncroachers=true
	bHidden=false
	NetUpdateFrequency=1.0
	SupportedEvents.Add(class'SeqEvent_PickupStatusChange')
	
	
}